#!/opt/homebrew/bin/python3.13
import re
import json
# 字符串

str1 = "str1\b23"
print(str1[0:7])
str2 = "ad"
print(str1 + str2)

strformat="{0} {1} {2}".format("hello","world","!")
print(strformat)

# 输入输出

def parse_input(text):
    text = re.sub(r'[^\w]', ' ', text)
    text = text.lower()
    word_list = text.split()
    word_dic = {}
    for word in word_list:
        if word in word_dic:
            word_dic[word] += 1
        else:
            word_dic[word] = 1

    sorted_word_dic = sorted(word_dic.items(), key=lambda x: x[1], reverse=True)
    return sorted_word_dic

with open('./data/nlp_word.txt', 'r') as fin:
    text = fin.read()

word_and_freq = parse_input(text)
with open('./data/nlp_word_out.txt', 'w') as fout:
    for word, freq in word_and_freq:
        fout.write('{} {}\n'.format(word, freq))


# json
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}
with open('./data/str_data.json', 'w') as fout:
    finally_data = json.dumps(data)
    fout.write(finally_data)

data_obj = ""
with open('./data/str_data.json', 'r') as fin:
    data_obj = json.loads(fin.read())

print(data_obj)








